package com.mgr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mgr.dao.AccountDao;
import com.mgr.model.Accounts;
import com.mgr.model.Customers;
import com.mgr.service.AccountService;

@Service
public class AccountServiceImpl implements AccountService {

	@Autowired
	private AccountDao accountDao;

	@Transactional
	public boolean updateBalance(int accId, int balance) {
		Accounts account = accountDao.getAccount(accId);
		boolean isUpdate = false;
		if (account != null) {
			int newBalance = account.getBalance() + balance;
			account.setBalance(newBalance);
			isUpdate = true;
		}
		return isUpdate;
	}

	@Transactional
	public boolean verifyBalance(int accId, int balance) {
		Accounts account = accountDao.getAccount(accId);
		return (account != null && account.getBalance() > balance);
	}

	@Transactional
	public List<Accounts> getAccountByCustId(int custId) {
		return accountDao.getAccountByCustId(custId);
	}
	
	@Transactional
	public Accounts getAccount(int accId) {
		return accountDao.getAccount(accId);
	}

	@Transactional
	public Customers getCustomerByAccId(int accId) {		
		return accountDao.getCustomerByAccId(accId);
	}

}
